var app = angular.module('app',[]).controller('goodsDetail',
		function($scope, $http, queue){
	
    /*商品数量操作*/
    function goodsCount(o){
            if(!(o instanceof Object)) var o={};
            var inputCell = o.inputCell || ".count-input",
                minusCell = o.minusCell || ".count-minus",
                plusCell = o.plusCell || ".count-plus",
                disClass = o.disClass || "disabled";
            return this.each(function(){
                var $wrap = $(this),
                    $input = $(inputCell,$wrap),
                    $minus = $(minusCell,$wrap),
                    $plus = $(plusCell,$wrap),
                    maxnum=parseInt($wrap.attr('data-max')) || false,
                    minnum=$wrap.attr('data-min') || 1,
                    initnum=$input.val() || minnum;
         
                $input.val(initnum);
                checkIlegal();
                function checkIlegal(){
                    var value =parseInt($input.val());

                    //
                     if (maxnum&&value>maxnum) {
                        $input.val(maxnum);
                    }
                    else if (value<minnum) {
                        $input.val(minnum);
                    }
                    if(value<=minnum){
                        $minus.addClass(disClass);
                    }else{
                        $minus.removeClass(disClass);
                    }
                    if (value>=maxnum) {
                        $plus.addClass(disClass);
                    }else {
                        $plus.removeClass(disClass);
                    }

                }
                function checknull() {
                    var value =$input.val();
                    if(value === "" || value === "0"){
                        $input.val(minnum);
                    }
                }
                $input.keyup(function(evt){
                    var value = $(this).val();
                    var newvalue = value.replace(/[^\d]/g,"");
                    $(this).val(newvalue);
                    checknull();
                });
                $input.blur(function(){
                    checknull();
                    checkIlegal();
                })

                $minus.click(function(){
                    minus();
                     checkIlegal();
                });

                $plus.click(function(){
                    add();
                    checkIlegal();
                });

                function add () {
                    var value = $input.val();
                    var plus = parseInt(value)+1;
                    $input.val(plus);
                }
                function minus () {
                    var value = parseInt($input.val());
                    var minus = value-1;
                    $input.val(minus);
                }
            });
        }
        $.fn.goodsCount = goodsCount;
        var getImg = function(q) {
        	$scope.img = '';
        	var href = location.href;
        	var reg = /img=(.*)&xm=(.*)&de=(.*)&num=(.*)&price=(.*)&cjlx=(.*)&cjcz=(.*)&sb=(.*)&ljcc=(.*)/;
        	var imga = href.match(reg);
        	if (imga) {
        		$scope.img = imga[1];
        		$scope.xm = decodeURI(imga[2]);
        		$scope.de = decodeURI(imga[3]);
        		$scope.num = imga[4];
        		$scope.price = imga[5];
        		$scope.cjlx = decodeURI(imga[6]);;
        		$scope.cjcz = decodeURI(imga[7]);;
        		$scope.sb = imga[8];
        		$scope.ljcc = imga[9]
        	
        	}
        	if (q)
				q.success();
        };
        var getSessionUser = function(q){ 
    		
  		   $http.post("./getSessionUser").success(function(data){
  			  
  			   $scope.username = data.username;
  			 $scope.obj = {"img":$scope.img,
         			"xm":$scope.xm,
         			"price":$scope.price,
         			"cjlx":$scope.cjlx,
         			"cjcz":$scope.cjcz,
         			"sb":$scope.sb,
         			"ljcc":$scope.ljcc,
         			"username":$scope.username
         			};
  			if (q)
				q.success();
  		   })
  		} 
       
       var isSc = function(q) {
    	   var obj1 = {
    			   "username":$scope.username,
    			   "xm":$scope.xm
    	   }
        	
        	$http.post("./isSc",obj1).success(function(data) {
        		if(data.status == "已收藏"){
        			$scope.sc = "已收藏";
        		}else if(data.status == "未收藏"){
        			$scope.sc = "收藏";
        		}
        		if (q)
    				q.success();
        	})
        }
        $scope.shouCang = function(){
        	if($scope.sc=="收藏"){
        		if($scope.username  == null || $scope.username == ''){
        			$scope.sc=="收藏"
        			alert("用户未登录,请登录！");
        		}else{
        			$http.post("./collection",$scope.obj).success(function(data) {
        				if(data.status == "ok"){
        					alert("收藏成功");
        					$scope.sc = "已收藏";
        				}
        			})
        		}
        	}else if($scope.sc=="已收藏") {
        		if($scope.username  == null || $scope.username == ''){
        			alert("用户未登录,请登录！");
        		}else{
        			$http.post("./qxCollection",$scope.obj).success(function(data) {
        				if(data.status == "ok"){
        					alert("取消成功");
        					$scope.sc = "收藏";
        				}
        			})
        		}
        	}
        	
        }

        
        
        $scope.logout = function(){
        	
        	$http.post("./logout1").success(function(data) {
        		if(data.status == "ok"){
					window.open("./userlogin");
				}
        		
        	})
        }
        
        
        $scope.addGwc = function() {
        	$scope.Gwc = {
            		"img":$scope.img,
            		"price":$scope.price,
            		"num":$("#num").val(),
            		"xm":$scope.xm
            }
        	$http.post("./addGwc",$scope.Gwc).success(function(data){
        		if(data.num == "超出数量"){
        			alert("一种自行车每人限租5辆");
        			return;
        		}else{
        			alert("添加购物车成功")
        			window.open("../wgc/cart");
        		}
        	})
        }
        
        
        $scope.rent = function() {
        	
        	$scope.rent = {
            		"img":$scope.img,
            		"price":$scope.price,
            		"num":$("#num").val(),
            		"xm":$scope.xm
            }
        	$http.post("./rent",$scope.rent).success(function(data) {
        		if(data.status == 'ok'){
        			window.open("../wgc/rent");
        		}
        	})
        }
        
        queue.add(getImg()).add(getSessionUser()).add(isSc());/*.start(function() {getSessionUser()});*/
})